package org.smart4j.chapter2.controller;

import com.sun.deploy.util.ArrayUtil;
import org.apache.commons.lang3.StringUtils;
import org.smart4j.chapter2.model.Customer;
import org.smart4j.chapter2.service.CustomerService;
import org.smart4j.chapter2.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by chinalin on 2017/3/13.
 */
@WebServlet("/customer")
public class CustomeServlet extends HttpServlet{
    private CustomerService customerService;

    @Override
    public void init() throws ServletException {
        this.customerService = new CustomerService();
    }


    public static String getString(InputStream is) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            while((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
//            LOGGER.error("get string failure", e);
            throw new RuntimeException(e);
        }
        return sb.toString();
    }

    /**
     * 测试req.getParameterNames()和req.getInputStream()
     * 当get提交时 req.getParameterNames()可以读到，而req.getInputStream()读不到参数
     * 当post提交时 req.getParameterNames()可以读到参数，读完后，req.getInputStream()读不到了，如果req.getParameterNames()没执行，那么req.getInputStream()就可以读到参数了
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String ,Object> paramMap = new HashMap<String, Object>();
//        Enumeration<String> paramNames = req.getParameterNames();
//        while(paramNames.hasMoreElements()) {
//            String paramName = paramNames.nextElement();
//            String paramValue = req.getParameter(paramName);
//            paramMap.put(paramName,paramValue);
//        }
        String body = getString(req.getInputStream()) ;
//        String body = CodecUtil.decodeURL(StreamUtil.getString(req.getInputStream()));
        if(StringUtil.isNotEmpty(body)) {
//            String[] params = com.sun.deploy.util.StringUtils.splitString(body, "&");
//            String[] params = body.split("&") ;
            String[] params = StringUtils.splitByWholeSeparator(body, "&");
            for (String param : params) {
//                    String[] array = param.split("=") ;
                    String[] array = StringUtils.splitByWholeSeparator(param, "=");
                    String paramName = array[0];
                    String paramValue = array[1];
                    paramMap.put(paramName, paramValue);
                }
        }
    }

    /**
     * 进入 客户列表界面
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Customer> customerList = customerService.getCustomerList() ;
        req.setAttribute("customerList", customerList);
        req.getRequestDispatcher("/WEB-INF/view/customer.jsp").forward(req,resp);
    }
}
